//
//  AttachTableViewCell.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-24.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "AttachTableViewCell.h"
#import "UIView+Addtions.h"

#define AccessoryViewWidth 40.0f
#define LeftImageSizeValue 35.0f
#define LevelIndentationValue 15.0f
#define TitleHeight 45.0f

@interface AttachTableViewCell ()

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *title;

@end

@implementation AttachTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_leftImageView];
        
        self.title = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_title];
        
        [self setDefaultsValue];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    //[self printSubViews:self withIndent:@" "];
    if (self.editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"style");
        [self printSubViews:self withIndent:@" "];
        if (self.showingDeleteConfirmation) {
            NSLog(@"style yes");
        }
    }
    // Configure the view for the selected state
}

- (void)printSubViews:(UIView *)superView withIndent:(NSString *)indent {

    NSLog(@"%@", [NSString stringWithFormat:@"%@:%@", indent, [superView class]]);
    NSArray *array = superView.subviews;
    if (array > 0) {
        for (UIView *V in array) {
            [self printSubViews:V withIndent:[NSString stringWithFormat:@"\t%@", indent]];
        }
    }
}

- (void)setDefaultsValue {
    _title.numberOfLines = 2;
}

static UIImage *_image = nil;
- (UIView *)expandableView
{
    if (!_image) {
        _image = [UIImage imageNamed:@"expandableImage.png"];
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, _image.size.width, _image.size.height);
    button.frame = frame;
    
    [button setBackgroundImage:_image forState:UIControlStateNormal];
    
    return button;
}

- (void)setAttachInfo:(AttachInfo *)attachInfo {
    _attachInfo = attachInfo;
    self.isDirectory = _attachInfo.isDirectory;
    _title.text = _attachInfo.fileName;
    
    //!!!!: if you want to add sth. in this function, please add before this line
    if (_isDirectory) {
        _isOpened = _attachInfo.isOpened;
        if (_isOpened) {
            self.accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
        } else {
            self.accessoryView.transform = CGAffineTransformMakeRotation(0);
        }
        _leftImageView.image = [UIImage imageNamed:@"filetype_folder_76h"];
        return;
    }
    
    NSString *filename = _attachInfo.fileName;
    _leftImageView.image = [UIImage imageNamed:[self.class getImageWithFileName:filename]];
}

+ (NSString *)getImageWithFileName:(NSString *)fileName {
    
    NSString *imageName;
    
    if ([fileName hasSuffix:@".doc"]||[fileName hasSuffix:@".docx"]) {
        imageName = @"filetype_word_76h";
    } else if([fileName hasSuffix:@"xls"]||[fileName hasSuffix:@".xlsx"]) {
        imageName = @"filetype_excel_76h";
    } else if ([fileName hasSuffix:@"ppt"]||[fileName hasSuffix:@".pptx"]) {
        imageName = @"filetype_ppt_76h";
    } else if([fileName hasSuffix:@"pdf"]) {
        imageName = @"filetype_pdf_76h";
    } else if([fileName hasSuffix:@"txt"]) {
        imageName = @"filetype_txt_76h";
    } else if([fileName hasSuffix:@"jpg"]||[fileName hasSuffix:@"png"]) {
        imageName = @"filetype_image_76h";
    } else if([fileName hasSuffix:@"mp4"]) {
        imageName = @"filetype_video_76h";
    } else {
        imageName = @"filetype_others_76h";
    }
    return imageName;
}

- (void)setIsDirectory:(BOOL)isDirectory {
    if (isDirectory) {
        [self setAccessoryView:[self expandableView]];
    } else {
        [self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    _isDirectory = isDirectory;
}

- (void)setIsOpened:(BOOL)isOpened {
    _isOpened = isOpened;
    if (_attachInfo) {
        _attachInfo.isOpened = isOpened;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat leftPadding = (self.height - LeftImageSizeValue)*0.5;
    _leftImageView.size = CGSizeMake(LeftImageSizeValue, LeftImageSizeValue);
    _leftImageView.left = leftPadding + LevelIndentationValue*_attachInfo.fileLevel;
    //NSLog(@"_attachInfo.fileLevel:%d", _attachInfo.fileLevel);
    _leftImageView.centerY = self.height*0.5;
    
    _title.left = _leftImageView.right + leftPadding;
    CGFloat titleWidth = self.width - AccessoryViewWidth - _title.left;
    _title.width = titleWidth;
    _title.height = TitleHeight;
    _title.centerY = self.height*0.5;
}

- (void)prepareForReuse {
    NSLog(@"-------");
    NSLog(@"%@", _attachInfo.fileName);
    NSLogBOOL(@"_isOpened", _isOpened);
    NSLogBOOL(@"_isDirectory", _isDirectory);
    _isOpened = NO;
    _isDirectory = NO;
    _attachInfo = nil;
    self.accessoryView = nil;
    self.accessoryType = UITableViewCellAccessoryNone;
    [self setEditing:NO];
    [super prepareForReuse];
}

@end
